using System;
using System.Collections.Generic;
using System.Data;
using METU_CMT.DataSets.METUCMTDataSetTableAdapters;

namespace METU_CMT.Business
{
    public class EventTableAdapter
    {
        public DateTime GetDueDate(int eventId)
        {
            EventsTableAdapter _eventsTableAdapter = new EventsTableAdapter();
            DataTable _eventDt = _eventsTableAdapter.GetDataByEventId(eventId);
            if (_eventDt.Rows.Count > 0)
            {
                bool _successfull;
                DateTime _dueDate = CommonMethods.ToDateTime(_eventDt.Rows[0]["DueDate"].ToString(),
                                                             "dd.MM.yyyy HH:mm:ss", out _successfull);

                if (_successfull == true)
                {
                    return _dueDate;
                }
            }
            return new DateTime();
        }

        public string GetDescription(int eventId)
        {
            EventsTableAdapter _eventsTableAdapter = new EventsTableAdapter();
            DataTable _eventDt = _eventsTableAdapter.GetDataByEventId(eventId);
            if (_eventDt.Rows.Count > 0)
            {
                return _eventDt.Rows[0]["Description"].ToString();
            }
            return string.Empty;
        }

        public bool GetSubmissionExists(int eventId)
        {
            EventsTableAdapter _eventsTableAdapter = new EventsTableAdapter();
            DataTable _eventDt = _eventsTableAdapter.GetDataByEventId(eventId);
            if (_eventDt.Rows.Count > 0)
            {
                return int.Parse(_eventDt.Rows[0]["SubmissionRequiredFlag"].ToString()) > 0;
            }
            return false;
        }

        public List<Event> GetAllEvents()
        {
            List<Event> _events = new List<Event>();
            EventsTableAdapter _eventsTableAdapter = new EventsTableAdapter();
            DataTable _eventDt = _eventsTableAdapter.GetData();
            foreach (DataRow _eventDr in _eventDt.Rows)
            {
                int _eventId;
                if (int.TryParse(_eventDr["EventId"].ToString(), out _eventId) == false)
                {
                    continue;
                }
                Event _event = new Event(_eventId);
                _events.Add(_event);
            }

            return _events;
        }

        public int GetMailState(int eventId)
        {
            EventsTableAdapter _eventsTableAdapter = new EventsTableAdapter();
            DataTable _eventDt = _eventsTableAdapter.GetDataByEventId(eventId);
            if (_eventDt.Rows.Count > 0)
            {
                int _mailState = 0;
                if (int.TryParse(_eventDt.Rows[0]["MailState"].ToString(), out _mailState) == false)
                {
                    return 0;
                }
                return _mailState;
            }
            return 0;
        }

        public Course GetCourseOfEvent(int eventId)
        {
            EventsTableAdapter _adapter = new EventsTableAdapter();

            DataTable _table = _adapter.GetDataByEventId(eventId);
            if (_table.Rows.Count > 0)
            {
                int _courseId;
                if (int.TryParse(_table.Rows[0]["ScheduleId"].ToString(), out _courseId) == false)
                {
                    return null;
                }
                return new Course(_courseId);
            }
            return null;
        }
    }
}